package com.qiwenshare.stock.executor;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson2.JSON;
import com.qiwenshare.stock.analysis.ReplayOperation;
import com.qiwenshare.stock.analysis.TechnologyAnalysis;
import com.qiwenshare.stock.analysis.indicator.KDJAnalysis;
import com.qiwenshare.stock.api.IGuessDailyService;
import com.qiwenshare.stock.api.IReplayService;
import com.qiwenshare.stock.api.IStockDayInfoService;
import com.qiwenshare.stock.common.DingTalkUtil;
import com.qiwenshare.stock.common.TaskProcess;
import com.qiwenshare.stock.controller.StockController;
import com.qiwenshare.stock.domain.GuessDailyBean;
import com.qiwenshare.stock.domain.ReplayBean;
import com.qiwenshare.stock.domain.StockBean;
import com.qiwenshare.stock.domain.StockDayInfo;
import com.qiwenshare.stock.websocket.StockWebsocket;

import java.util.Collections;
import java.util.Date;
import java.util.List;

public class ReplayRunnable implements Runnable {

    public static volatile int updateCount = 0;
    public static int totalCount = 0;
    public StockBean stockBean;

    IStockDayInfoService stockDayInfoService;

    IReplayService replayService;

    IGuessDailyService guessDailyService;

    public ReplayRunnable(StockBean stockBean, IReplayService replayService, IStockDayInfoService stockDayInfoService,
                          IGuessDailyService guessDailyService) {
        this.stockBean = stockBean;
        this.stockDayInfoService = stockDayInfoService;
        this.replayService = replayService;
        this.guessDailyService = guessDailyService;
    }

    @Override
    public void run() {
        if (!StockController.stockReplayexec.isShutdown()) {
            TaskProcess taskProcess = new TaskProcess();
            //查询是升序
            List<StockDayInfo> stockDayInfoList = stockDayInfoService.getStockdaybar(stockBean.getStockNum());
//            Collections.reverse(stockDayInfoList);
            if(!CollectionUtil.isEmpty(stockDayInfoList)) {
                replayService.deleteReplay(stockBean.getStockNum());

                List<ReplayBean> replayList = new ReplayOperation().getReplayInfo(stockDayInfoList, stockBean);
                replayService.insertReplay(replayList);
                if(stockDayInfoList.size() < 1) {
                    return;
                }
//                DingTalkUtil.send(null, JSON.toJSONString(stockDayInfoList.get(0)));

                //新增超买，超卖数据
                TechnologyAnalysis kdjAnalysis = new KDJAnalysis();
                StockDayInfo stockDayInfo =  stockDayInfoList.get(stockDayInfoList.size() - 1);
                ReplayBean kdjReplayBean = kdjAnalysis.getOperation(ListUtil.of(stockDayInfo), stockBean);
                if(kdjReplayBean != null && (kdjReplayBean.getLiteBuy() == 1 || kdjReplayBean.getLiteSell() == 1)) {
                    GuessDailyBean guessDailyBean = buildGuessDaily(kdjReplayBean);
                    guessDailyBean.setPriceDate(stockDayInfo.getDate());
                    guessDailyBean.setStockNum(stockBean.getStockNum());
                    guessDailyBean.setK(stockDayInfo.getK());
                    guessDailyBean.setD(stockDayInfo.getD());
                    guessDailyBean.setJ(stockDayInfo.getJ());
                    guessDailyBean.setCci(stockDayInfo.getCci());
                    guessDailyService.insertGuessDaily(guessDailyBean);
                }

            }


            synchronized (StockController.class) {
                updateCount++;
            }


            taskProcess.setCompleteCount(updateCount);
            taskProcess.setTotalCount(totalCount);
            taskProcess.setTaskInfo("复盘项：" + stockBean.getStockNum() + "完成进度：" + updateCount + "/" + totalCount);
            taskProcess.setRunTask(totalCount != updateCount);
//            StockWebsocket.pushTaskProcess(taskProcess);
        }
    }

    private GuessDailyBean buildGuessDaily(ReplayBean replayBean) {
        GuessDailyBean guessDailyBean = new GuessDailyBean();
        guessDailyBean.setCreateTime(new Date());
        guessDailyBean.setLiteBuy(replayBean.getLiteBuy());
        guessDailyBean.setLiteSell(replayBean.getLiteSell());
        guessDailyBean.setStockNum(replayBean.getStockNum());
        return guessDailyBean;
    }
}
